'''
Created on Nov 20, 2011

@author: jonathan
'''

from Game import Game
import Settings

from google.appengine.ext import db

class Paper(db.Model):
    game = db.ReferenceProperty(Game, collection_name="papers")
    user = db.UserProperty(required=True, auto_current_user_add=True)
    order = db.IntegerProperty(required=True)
    
    def AddItem(self, paperItem):
        paperItem.order = 0
        latestItem = self.LatestItem()
        if latestItem:
            paperItem.order = latestItem.order + 1
        paperItem.paper = self
        paperItem.put()
    
    def Items(self, page=0):
        return self.items.order("+order").fetch(Settings.REQUEST_LIMIT, Settings.REQUEST_LIMIT * page)
    
    def LatestItem(self):
        return self.Items.order("-order").get()